package com.gxa.insurance.controller;

import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.pojo.Admin;
import com.gxa.insurance.pojo.Permission;
import com.gxa.insurance.service.AdminService;
import com.gxa.insurance.util.JsonResult;
import com.gxa.insurance.util.StringUtil;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.awt.print.Pageable;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author Min
 */
@RestController
@RequestMapping("/admin")
@CrossOrigin
@Api("管理员用户的控制类")
public class AdminController {
    @Autowired
    private AdminService service;

    @ApiOperation(value = "登录接口",produces = "query")
    @PostMapping ("/login")
    public Map login(@RequestParam("username")String username, @RequestParam("password")String password,
                    /*@RequestParam("message")String message,*/@RequestParam("phone")String phone,HttpServletRequest request) throws Exception {
         JsonResult jsonResult = new JsonResult();
//         String sysMessage = (String) request.getSession().getAttribute("captcha");
//         String sysPhone=(String) request.getSession().getAttribute("phone");
////          验证短信信息
//         if(StringUtil.isEmpty(message) || StringUtil.isEmpty(sysMessage) || !sysMessage.equals(message)  || StringUtil.isEmpty(sysPhone)){
//            throw new BusinessException(ExceptionCode.WRONG_MESSAGE);
//         }
         if(StringUtil.isEmpty(username) || StringUtil.isEmpty(password) || StringUtil.isEmpty(phone) || !StringUtil.isPhoneNumber(phone)){
             throw new BusinessException(ExceptionCode.WRONG_VALUE);
         }
        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setPassword(password);
        admin.setPhone(phone);
        boolean isOk = service.login(admin);
        if(isOk){
            // 产生一个token
            String token = UUID.randomUUID().toString().replace("-", "");
            // 将token存在session当中
            HttpSession session = request.getSession();
            session.setAttribute("token",token);
            jsonResult.setCode("1");
            jsonResult.setMsg("登录成功");
            jsonResult.put("token",token);
        }
        return jsonResult.getMap();
    }

    @ApiOperation(value = "通过用户手机号获取本用户的权限树的接口",produces = "query")
    @PostMapping("/tree")
    public Map getAdminTree(@RequestParam(name = "phone")String phone){
        JsonResult jsonResult = new JsonResult();
        List<Permission> permission=service.selectTreeByphone(phone);
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        jsonResult.setData(permission);
        return jsonResult.getMap();
    }

    @ApiOperation("查找所有管理员")
    @PostMapping("/selectAll")
    public Map selectAllByPage(@RequestParam("page")String page,@RequestParam("pageSize")String pageSize) throws BusinessException {
        if(StringUtil.isEmpty(page) || StringUtil.isEmpty(pageSize)|| !StringUtil.isNumber(page) || !StringUtil.isNumber(pageSize))
        {
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        JsonResult jsonResult = new JsonResult();
        Map<String,Object> map=service.selectAllByPage(page,pageSize);
        jsonResult.setMsg("查询成功");
        jsonResult.setCode("1");
        jsonResult.setData(map.get("list"));
        jsonResult.put("count",map.get("count"));
        return jsonResult.getMap();
    }

    @ApiOperation(value = "新建管理账户/新建账号",produces = "data")
    @ApiImplicitParam(name = "admin",dataType = "Admin",value = "管理员json 角色id  超级管理员 - 1，admin -2  ，普通管理员 - 3")
    @PostMapping("/add")
    public Map addAdmin(@RequestBody Admin admin) throws Exception {
        JsonResult jsonResult = new JsonResult();
        if(admin==null){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        Boolean isOk=service.addAdmin(admin);
        if(isOk){
            jsonResult.setCode("1");
            jsonResult.setMsg("添加成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("添加失败");
        }
        return jsonResult.getMap();
    }

    @ApiOperation(value = "修改管理员用户",produces = "data")
    @ApiImplicitParam(name = "admin",value = "管理员json",dataType = "Admin")
    @PostMapping("/update")
    public Map updateAdmin(@RequestBody Admin admin) throws Exception {
        JsonResult jsonResult = new JsonResult();
        if(admin==null){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        Boolean isOk=service.updateAdmin(admin);
        if(isOk){
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("修改失败");
        }
        return jsonResult.getMap();
    }
    @ApiOperation("修改是否启用改用户")
    @GetMapping("/updateEnable")
    public Map updateISEnable(@RequestParam("id")Integer id, @RequestParam("isEnable")Boolean isEnable) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        if(isEnable==null || id<=0 || id==null){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        Boolean isOk=service.updateIsEnable(id,isEnable);
        if(isOk){
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("修改失败");
        }
        return jsonResult.getMap();
    }

    @ApiOperation("删除管理员用户的接口")
    @ApiImplicitParam(name = "id" ,value = "要删除的id",dataType = "integer")
    @GetMapping("/delete")
    public Map deleteAdmin(@RequestParam("id")Integer id){
        JsonResult jsonResult = new JsonResult();
        Boolean isOk=service.deleteAdminById(id);
        if(isOk){
            jsonResult.setCode("1");
            jsonResult.setMsg("删除成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("删除失败");
        }
        return jsonResult.getMap();
    }
}
